import os
import extract_msg


def read_fujian(file_name):
    msg = extract_msg.Message(file_name)
    email_name = os.path.splitext(file_name)[0]  # 邮件名称
    for attachment in msg.attachments:
        attachment_name = attachment.longFilename

        new_attachment_name = f"{email_name}_{attachment_name}"  # 新的附件名称
        attachment.save(customPath='.\\2022 shurlok', customFilename=None)

        original_attachment_path = os.path.join(path, attachment_name)
        os.rename(original_attachment_path, new_attachment_name)


def getFileName(path):
    # 获取指定目录下的所有指定后缀的文件名
    f_list = os.listdir(path)
    for i in f_list:
        if os.path.splitext(i)[1] == '.msg':
            # print(i)
            # 保存所有附件
            read_fujian(os.path.join(path, i))


if __name__ == '__main__':
    path = '.\\2022 shurlok'
    getFileName(path)
